﻿using SD.Common;
using SD.Infrastructure.DTOBase;
using SD.Infrastructure.WPF.Caliburn.Aspects;
using SD.Infrastructure.WPF.Caliburn.Base;
using SD.Infrastructure.WPF.Extensions;
using SD.Infrastructure.WPF.Models;
using SlamDunk.MES.IAppService.DTOs.Outputs.PlanContext;
using SlamDunk.MES.IAppService.Interfaces;
using SlamDunk.ValueObjects.Enums;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.ServiceModel.Extensions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace SlamDunk.MES.Client.ViewModels.ProductionPlan
{
    /// <summary>
    /// 生产计划选择视图模型
    /// </summary>
    public class SelectProductionPlanViewModel : ScreenBase
    {
        #region # 字段及构造器

        /// <summary>
        /// 资源定义服务契约接口代理
        /// </summary>
        private readonly ServiceProxy<IPlanContract> _planContract;

        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        public SelectProductionPlanViewModel(ServiceProxy<IPlanContract> resourceContract)
        {
            this._planContract = resourceContract;

            //默认值
            this.PageIndex = 1;
            this.PageSize = 20;
        }

        #endregion

        #region # 属性

        #region 关键字 —— string Keywords
        /// <summary>
        /// 关键字
        /// </summary>
        [DependencyProperty]
        public string Keywords { get; set; }
        #endregion

        #region 开始时间 —— DateTime? StartTime
        /// <summary>
        /// 开始时间
        /// </summary>
        [DependencyProperty]
        public DateTime? StartTime { get; set; }
        #endregion

        #region 结束时间 —— DateTime? EndTime
        /// <summary>
        /// 结束时间
        /// </summary>
        [DependencyProperty]
        public DateTime? EndTime { get; set; }
        #endregion

        #region 页码 —— int PageIndex
        /// <summary>
        /// 页码
        /// </summary>
        [DependencyProperty]
        public int PageIndex { get; set; }
        #endregion

        #region 页容量 —— int PageSize
        /// <summary>
        /// 页容量
        /// </summary>
        [DependencyProperty]
        public int PageSize { get; set; }
        #endregion

        #region 总记录数 —— int RowCount
        /// <summary>
        /// 总记录数
        /// </summary>
        [DependencyProperty]
        public int RowCount { get; set; }
        #endregion

        #region 总页数 —— int PageCount
        /// <summary>
        /// 总页数
        /// </summary>
        [DependencyProperty]
        public int PageCount { get; set; }
        #endregion

        #region 已选计划状态 —— ProductionPlanStatus? SelectedPlanStatus
        /// <summary>
        /// 已选计划状态
        /// </summary>
        [DependencyProperty]
        public ProductionPlanStatus? SelectedPlanStatus { get; set; }
        #endregion

        #region 生产计划列表 —— ObservableCollection<Wrap<ProductionPlanInfo>> ProductionPlans
        /// <summary>
        /// 生产计划列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<Wrap<ProductionPlanInfo>> ProductionPlans { get; set; }
        #endregion

        #region 计划状态字典 —— IDictionary<string, string> PlanStatuses
        /// <summary>
        /// 计划状态字典
        /// </summary>
        [DependencyProperty]
        public IDictionary<string, string> PlanStatuses { get; set; }
        #endregion

        #region 是否允许多选 —— bool MultipleCheck
        /// <summary>
        /// 是否允许多选
        /// </summary>
        public bool MultipleCheck { get; set; }
        #endregion

        #region 工厂模型Id —— Guid? FacilityId
        /// <summary>
        /// 工厂模型Id
        /// </summary>
        public Guid? FacilityId { get; set; }
        #endregion

        #endregion

        #region # 方法

        //Initialization

        #region 初始化 —— override async Task OnInitializeAsync(CancellationToken cancellationToken)
        /// <summary>
        /// 初始化
        /// </summary>
        protected override async Task OnInitializeAsync(CancellationToken cancellationToken)
        {
            this.PlanStatuses = typeof(ProductionPlanStatus).GetEnumMembers();

            await this.ReloadProductionPlans();
        }
        #endregion

        #region 加载 —— void Load(bool multipleCheck, ProductionPlanStatus? planStatus...
        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="multipleCheck">是否允许多选</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="planStatus">生产计划状态</param>
        public void Load(bool multipleCheck, ProductionPlanStatus? planStatus, Guid? facilityId)
        {
            this.MultipleCheck = multipleCheck;
            this.FacilityId = facilityId;

            if (planStatus.HasValue)
            {
                this.SelectedPlanStatus = planStatus;
            }
        }
        #endregion


        //Actions

        #region 加载生产计划列表 —— async void LoadProductionPlans()
        /// <summary>
        /// 加载生产计划列表
        /// </summary>
        public async void LoadProductionPlans()
        {
            await this.ReloadProductionPlans();
        }
        #endregion

        #region 全选 —— void CheckAll()
        /// <summary>
        /// 全选
        /// </summary>
        public void CheckAll()
        {
            this.ProductionPlans.ForEach(x => x.IsChecked = true);
        }
        #endregion

        #region 取消全选 —— void UncheckAll()
        /// <summary>
        /// 取消全选
        /// </summary>
        public void UncheckAll()
        {
            this.ProductionPlans.ForEach(x => x.IsChecked = false);
        }
        #endregion

        #region 提交 —— async void Submit()
        /// <summary>
        /// 提交
        /// </summary>
        public async void Submit()
        {
            Wrap<ProductionPlanInfo>[] checkedPlans = this.ProductionPlans.Where(x => x.IsChecked == true).ToArray();

            if (!this.MultipleCheck)
            {
                if (checkedPlans.Length != 1)
                {
                    MessageBox.Show("请勾选一条生产计划！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
            }
            else
            {
                if (!checkedPlans.Any())
                {
                    MessageBox.Show("请至少勾选一条生产计划！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
            }

            await base.TryCloseAsync(true);
        }
        #endregion


        //Private

        #region 加载生产计划列表 —— async Task ReloadProductionPlans()
        /// <summary>
        /// 加载生产计划列表
        /// </summary>
        private async Task ReloadProductionPlans()
        {
            this.Busy();

            PageModel<ProductionPlanInfo> pageModel = await Task.Run(() => this._planContract.Channel.GetProductionPlansByPage(this.Keywords, null, null, null, this.SelectedPlanStatus, CheckStatus.Passed, this.StartTime, this.EndTime, this.FacilityId, 1, int.MaxValue));
            this.RowCount = pageModel.RowCount;
            this.PageCount = pageModel.PageCount;
            this.ProductionPlans = new ObservableCollection<Wrap<ProductionPlanInfo>>(pageModel.Datas.Select(x => x.Wrap()));

            this.Idle();
        }
        #endregion

        #endregion
    }
}
